package com.heima.wemadia.service.impl;

import com.github.pagehelper.PageHelper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.ChannelDto;
import com.heima.model.wemedia.entity.WmChannel;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.wemadia.dao.WmChannelDao;
import com.heima.wemadia.service.WmChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 自媒体频道表(WmChannel)表服务实现类
 *
 * @author makejava
 * @since 2022-04-12 15:37:58
 */
@Service("wmChannelService")
public class WmChannelServiceImpl implements WmChannelService {
    @Resource
    private WmChannelDao wmChannelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmChannel queryById(Long id) {
        return this.wmChannelDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param wmChannel 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<WmChannel> queryByPage(WmChannel wmChannel, PageRequest pageRequest) {
        long total = this.wmChannelDao.count(wmChannel);
        return new PageImpl<>(this.wmChannelDao.queryAllByLimit(wmChannel, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param wmChannel 实例对象
     * @return 实例对象
     */
    @Override
    public WmChannel insert(WmChannel wmChannel) {
        this.wmChannelDao.insert(wmChannel);
        return wmChannel;
    }

    /**
     * 修改数据
     *
     * @param wmChannel 实例对象
     * @return 实例对象
     */
    @Override
    public WmChannel update(WmChannel wmChannel) {
        this.wmChannelDao.update(wmChannel);
        return this.queryById(wmChannel.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmChannelDao.deleteById(id) > 0;
    }

    @Override
    public ResponseResult list() {

        List<WmChannel> channelList = wmChannelDao.queryAll();
        return ResponseResult.okResult(channelList);
    }


    @Override
    public ResponseResult ChannelList(ChannelDto dto) {

        dto.checkParam();
        PageHelper.startPage(dto.getPage(),dto.getSize());
        com.github.pagehelper.Page<WmSensitive> page= wmChannelDao.list(dto.getName());
        ResponseResult result = new PageResponseResult(page.getPageNum(), page.getPageSize(), (int) page.getTotal());

        result.setData(page);
        return result;
    }

    @Override
    public ResponseResult save(WmChannel wmChannel) {
        if (StringUtils.isBlank(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
       WmChannel channel= wmChannelDao.queryByName(wmChannel.getName());
        if(channel != null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称已存在");
        }
        channel.setName(wmChannel.getName());
        channel.setCreatedTime(new Date());
        channel.setUpdatedTime(new Date());
        if(wmChannel.getOrd() != null){
            channel.setOrd(wmChannel.getOrd());
        }else {
            channel.setOrd(0);
        }
        if(wmChannel.getDescription()!=null){
            channel.setDescription(wmChannel.getDescription());
        }
        if (wmChannel.getIsDefault()!=null) {
            channel.setIsDefault(wmChannel.getIsDefault());
        }else {
            channel.setIsDefault(0);
        }
        if(wmChannel.getStatus()!=null){
            channel.setStatus(wmChannel.getStatus());
        }else {
            channel.setStatus(1);
        }
        wmChannelDao.update(channel);
        return ResponseResult.okResult("添加成功");
    }
}
